#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=1e5+10;
int read()
{
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x*f;
}
int n,m,f[MAXN],cnt;
struct edge
{
	int u,v;
	bool operator<(const edge b)const
	{
		return v==b.v?u<b.u:v<b.v;
	}
}e[MAXN*2];
void addedge(int x,int y)
{
	e[++cnt]=(edge){x,y};
	return;
}
int main()
{
	n=read();m=read();
	for(int i=1;i<=m;++i)
	{
		int x=read(),y=read();
		addedge(min(x,y),max(x,y));
	}
	sort(e+1,e+1+m);
	int j=1;
	for(int i=1;i<=n;++i)
	{
		f[i]=f[i-1];
		for(;j<=m&&e[j].v==i;++j)
			if(e[j].u!=i-1)f[i]=max(f[i],f[e[j].u]+1);
	}
	printf("%d\n",f[n]+n-1);
	return 0;
}
